/**
 * @file lv_demo_keypad_encoder.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "../../lv_demos/lv_demo.h"


 /**
 * yby factory test
 *
 */

 /*********************
 *      INCLUDES
 *********************/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ft_main.h"


#ifdef YBY_LVGL_USE_HW_KEY
lv_indev_t* g_keypad_drv = NULL;
#endif
 //all result
uint8_t g_item_result[15] = {
	0x0,  //0 version 
	0x0,  //1 lcd
	0x0,  //2 tp
	0x0,  //3 key
	0x0,  //4 audio
	0x0,  //5 ble
	0x0,  //6 ir
	0x0,  //7 gsensor
	0x0,  //8 433
	0x0, //9 blacklight
	0x0, //10 vibrate
	0x0, //11 wifi
	0x0,//12 battery
	0x0
};


//static lv_obj_t *scr_main=NULL;
static lv_obj_t *win_main = NULL;
//static lv_obj_t *close_btn;
static lv_obj_t *list_main;
static lv_obj_t *btn_lcd, *btn_version, *btn_tp, *btn_key, *btn_audio, *btn_ble, *btn_ir, *btn_433, *btn_gsensor;
static lv_obj_t *btn_blacklight, *btn_vibrate, *btn_wifi, *btn_battery;


void ft_main_refresh_list(void *data)
{
	lv_event_send(list_main, LV_EVENT_VALUE_CHANGED, data);
}

static void ft_main_destory(void)
{
	printf("ft_main_destory + \n");
	if (win_main != NULL) {
		lv_obj_del(win_main);
		win_main = NULL;
	}

}

static void event_handler(lv_event_t *event)
{
	uint32_t data = 0;
	data = (uint32_t)lv_event_get_user_data(event);
	if (event->code == LV_EVENT_CLICKED) {
		printf("[Ft main]Clicked: data=%ld\n", data);

		switch (data)
		{
		case 0x00:

			break;
		case 0x01:

			//ft_lcd_create();
			break;
		case 0x02:
			//ft_tp_create();
			break;
		case 0x03:
			//ft_key_create();
			break;
		case 0x04:
			ft_audio_create();
			break;
		case 0x05:
			break;
		case 0x06:
			break;
		case 0x07:
			break;
		case 0x08:
			break;
		case 0x09:
			ft_bl_create();
			break;
		case 0x0b:
			ft_wifi_create();
			break;

		case 0xff:
			ft_main_destory();
			break;

		default:
			break;
		}
	}
}


static void result_event_handler(lv_event_t *event)
{
	uint32_t data = 0;
	data = (uint32_t)lv_event_get_param(event);

	if (event->code == LV_EVENT_VALUE_CHANGED) {
		printf("result_event_handler LV_EVENT_VALUE_CHANGED data=%ld\n", data);
		switch (data)
		{
		case 0:
			lv_obj_set_style_bg_color(btn_version, lv_palette_main(LV_PALETTE_GREEN), 0);
			break;
		case 1:
			if (g_item_result[1]) {
				lv_obj_set_style_bg_color(btn_lcd, lv_palette_main(LV_PALETTE_GREEN), 0);
			}
			else {
				lv_obj_set_style_bg_color(btn_lcd, lv_palette_main(LV_PALETTE_RED), 0);
			}
			break;
		case 2:
			if (g_item_result[2]) {
				lv_obj_set_style_bg_color(btn_tp, lv_palette_main(LV_PALETTE_GREEN), 0);
			}
			else {
				lv_obj_set_style_bg_color(btn_tp, lv_palette_main(LV_PALETTE_RED), 0);
			}
			break;
		case 3:
			if (g_item_result[3]) {
				lv_obj_set_style_bg_color(btn_key, lv_palette_main(LV_PALETTE_GREEN), 0);
			}
			else {
				lv_obj_set_style_bg_color(btn_key, lv_palette_main(LV_PALETTE_RED), 0);
			}
			break;
		case 4:
			if (g_item_result[4]) {
				lv_obj_set_style_bg_color(btn_audio, lv_palette_main(LV_PALETTE_GREEN), 0);
			}
			else {
				lv_obj_set_style_bg_color(btn_audio, lv_palette_main(LV_PALETTE_RED), 0);
			}
			break;
		case 5:
			if (g_item_result[5]) {
				lv_obj_set_style_bg_color(btn_ble, lv_palette_main(LV_PALETTE_GREEN), 0);
			}
			else {
				lv_obj_set_style_bg_color(btn_ble, lv_palette_main(LV_PALETTE_RED), 0);
			}
			break;
		case 6:
			if (g_item_result[6]) {
				lv_obj_set_style_bg_color(btn_ir, lv_palette_main(LV_PALETTE_GREEN), 0);
			}
			else {
				lv_obj_set_style_bg_color(btn_ir, lv_palette_main(LV_PALETTE_RED), 0);
			}
			break;
		case 7:
			if (g_item_result[7]) {
				lv_obj_set_style_bg_color(btn_gsensor, lv_palette_main(LV_PALETTE_GREEN), 0);
			}
			else {
				lv_obj_set_style_bg_color(btn_gsensor, lv_palette_main(LV_PALETTE_RED), 0);
			}
			break;
		case 8:
			if (g_item_result[8]) {
				lv_obj_set_style_bg_color(btn_433, lv_palette_main(LV_PALETTE_GREEN), 0);
			}
			else {
				lv_obj_set_style_bg_color(btn_433, lv_palette_main(LV_PALETTE_RED), 0);
			}
			break;
		case 9:
			if (g_item_result[9]) {
				lv_obj_set_style_bg_color(btn_blacklight, lv_palette_main(LV_PALETTE_GREEN), 0);
			}
			else {
				lv_obj_set_style_bg_color(btn_blacklight, lv_palette_main(LV_PALETTE_RED), 0);
			}
			break;
		default:
			break;
		}

	}
}

static void ft_main_init_view(void)
{
	printf("ft_main_init_view + \n");
	//scr_main = lv_obj_create(NULL);
	//lv_scr_load(scr_main);

	win_main = lv_win_create(lv_scr_act(), 50);
	lv_obj_set_size(win_main, 240, 320);
	lv_obj_t *title = lv_win_add_title(win_main, "Factory Test");
	lv_obj_align(title, LV_ALIGN_CENTER, 100, 0);

	//close_btn = lv_win_add_btn(win_main, LV_SYMBOL_CLOSE,46); 

	list_main = lv_list_create(win_main);
	lv_obj_set_size(list_main, 240, 320);
	lv_obj_align(list_main, LV_ALIGN_CENTER, 0, 0);

	lv_obj_add_event_cb(list_main, result_event_handler, LV_EVENT_VALUE_CHANGED, (void *)0x01);
	//add btns
	//lv_obj_t * list_btn;

	btn_version = lv_list_add_btn(list_main, LV_SYMBOL_LIST, "Version");
	lv_obj_add_event_cb(btn_version, event_handler, LV_EVENT_CLICKED, (void *)0x00);


	btn_lcd = lv_list_add_btn(list_main, LV_SYMBOL_LIST, "Lcd Test");
	lv_obj_add_event_cb(btn_lcd, event_handler, LV_EVENT_CLICKED, (void *)0x01);

	btn_tp = lv_list_add_btn(list_main, LV_SYMBOL_LIST, "Tp Test");
	lv_obj_add_event_cb(btn_tp, event_handler, LV_EVENT_CLICKED, (void *)0x02);

	btn_key = lv_list_add_btn(list_main, LV_SYMBOL_KEYBOARD, "Key Test");
	lv_obj_add_event_cb(btn_key, event_handler, LV_EVENT_CLICKED, (void *)0x03);

	btn_audio = lv_list_add_btn(list_main, LV_SYMBOL_AUDIO, "Audio loop Test");
	lv_obj_add_event_cb(btn_audio, event_handler, LV_EVENT_CLICKED, (void *)0x04);

	btn_ble = lv_list_add_btn(list_main, LV_SYMBOL_LIST, "Ble Test");
	lv_obj_add_event_cb(btn_ble, event_handler, LV_EVENT_CLICKED, (void *)0x05);

	btn_ir = lv_list_add_btn(list_main, LV_SYMBOL_LIST, "Ir Test");
	lv_obj_add_event_cb(btn_ir, event_handler, LV_EVENT_CLICKED, (void *)0x06);

	btn_gsensor = lv_list_add_btn(list_main, LV_SYMBOL_LIST, "Gsensor Test");
	lv_obj_add_event_cb(btn_gsensor, event_handler, LV_EVENT_CLICKED, (void *)0x07);

	btn_433 = lv_list_add_btn(list_main, LV_SYMBOL_LIST, "433 Test");
	lv_obj_add_event_cb(btn_433, event_handler, LV_EVENT_CLICKED, (void *)0x08);

	
	btn_blacklight = lv_list_add_btn(list_main, LV_SYMBOL_POWER, "Blacklight Test");
	lv_obj_add_event_cb(btn_blacklight, event_handler, LV_EVENT_CLICKED, (void *)0x09);

	btn_vibrate = lv_list_add_btn(list_main, LV_SYMBOL_LIST, "Vibrate Test");
	lv_obj_add_event_cb(btn_vibrate, event_handler, LV_EVENT_CLICKED, (void *)0x0a);

	btn_wifi = lv_list_add_btn(list_main, LV_SYMBOL_WIFI, "WiFi Test");
	lv_obj_add_event_cb(btn_wifi, event_handler, LV_EVENT_CLICKED, (void *)0x0b);
	
	btn_battery = lv_list_add_btn(list_main, LV_SYMBOL_CHARGE, "Battery Test");
	lv_obj_add_event_cb(btn_battery, event_handler, LV_EVENT_CLICKED, (void *)0x0c);
	
	lv_obj_t *btn_exit = lv_list_add_btn(list_main, LV_SYMBOL_CLOSE, "Exit");
	lv_obj_add_event_cb(btn_exit, event_handler, LV_EVENT_CLICKED, (void *)0xff);



#ifdef YBY_LVGL_USE_HW_KEY
	lv_group_t*  g = lv_group_create();
	//keypad group
	lv_group_add_obj(g, btn_version);
	lv_group_add_obj(g, btn_lcd);
	lv_group_add_obj(g, btn_tp);
	lv_group_add_obj(g, btn_key);
	lv_group_add_obj(g, btn_audio);
	lv_group_add_obj(g, btn_ble);
	lv_group_add_obj(g, btn_ir);
	lv_group_add_obj(g, btn_gsensor);
	lv_group_add_obj(g, btn_433);
	lv_group_add_obj(g, btn_blacklight);
	lv_group_add_obj(g, btn_vibrate);
	lv_group_add_obj(g, btn_exit);

	if (g_keypad_drv != NULL) {
		lv_indev_set_group(g_keypad_drv, g);
	}
#endif

}


void ft_main_create(void)
{
	printf("ft_main_create + \n");
	ft_main_init_view();

}
